﻿/*
-----------------------------------------------------------------------------
This source file is part of Cell Cloud.

Copyright (c) 2009-2012 Cell Cloud Team - cellcloudproject@gmail.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-----------------------------------------------------------------------------
*/

#ifndef CellBlockingAcceptor_h
#define CellBlockingAcceptor_h

#include "CellPrerequisites.h"
#include "CellMessageAcceptor.h"
#include "CellInet4Address.h"

namespace cell
{
/** \addtogroup Core
 *  @{
 */
/** \addtogroup General
 *  @{
 */

// 前置声明
class BADaemonThread;
class BARecvThread;

/** 阻塞式接收器。
@author
	Jiangwei Xu
*/
class _CellExport BlockingAcceptor : public MessageAcceptor, public GeneralAlloc
{
public:
	/** 构造函数。
	*/
	BlockingAcceptor(void);

	/** 析构函数。
	*/
	virtual ~BlockingAcceptor(void);

public:
	/**
	*/
	bool bind(uint port);

	/**
	*/
	bool bind(const InetAddress& address);

	/**
	*/
	void unbind(void);

	/**
	*/
	void write(Session* session, Message* message);

	/**
	*/
	void read(Message* message, Session* session);

	/**
	*/
	void close(Session* session);

	/**
	*/
	void listSessionIds(SessionIdList& list);

	/** 返回指定 ID 的 Session
	*/
	Session* getSession(ulong id);

	/**
	*/
	inline int getMaxConnectNum(void) { return mMaxConnectNum; }

	/**
	*/
	void setMaxConnectNum(int num);

	/**
	*/
	int getConnectNum(void);

	/**
	*/
	uint getPort(void) const;

protected:

	/**
	*/
	struct SessionCapsule : public GeneralAlloc
	{
		SessionCapsule(void);
		~SessionCapsule(void);

		socket_t sock;
		BARecvThread* thread;
		std::list<char*> buffer;
		CELL_MUTEX(mutex);
	};

	/**
	*/
	Session* fireSessionCreated(socket_t sock, const char* host, const uint port);

	/**
	*/
	void fireSessionDestroyed(Session* session, SessionCapsule* capsule);

	/**
	*/
	void fireSessionOpened(Session* session);

	/**
	*/
	void fireSessionClosed(Session* session);

	/**
	*/
	void fireMessageReceived(Session* session, Message* message);

	/**
	*/
	void fireMessageSent(Session* session, Message* message);

	/**
	*/
	void fireErrorOccurred(uint16 errorCode, Session* session);

private:
	/**
	*/
	bool startRecvThread(Session* session);

	/**
	*/
	void destroyRecvThreadDelay(BARecvThread* thread);

	/**
	*/
	socket_t getSock(Session* session);

	/**
	*/
	void forceClearupSessions(void);

private:
	typedef HashMap<Session*, SessionCapsule*> Sessions;
	typedef std::vector<BARecvThread*> RecvThreadList;

	struct ExpiredSession : public GeneralAlloc
	{
		ExpiredSession(void);
		~ExpiredSession(void);

		Session* session;
		SessionCapsule* capsule;
		uint64 time;
	};
	typedef std::vector<ExpiredSession*> ExpiredSessionList;

	CELL_MUTEX(mSessionMutex);
	CELL_MUTEX(mExpiredThreadMutex);
	//CELL_MUTEX(mExpiredSessionMutex);

	int mMaxConnectNum;
	int mBufSize;

	socket_t mListener;
	Inet4Address mV4Address;

	Sessions mSessions;
	Sessions mDesertedSessions;
	RecvThreadList mExpiredThreads;

	ExpiredSessionList mExpiredSessions;

	BADaemonThread* mThread;
	ExecutorService* mExecutor;

	friend class BADaemonThread;
	friend class BARecvThread;
};

/** @} */
/** @} */
} // end namespace cell

#endif // CellBlockingAcceptor_h
